package com.hzhr.iot.web.controller;

import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.user.sdk.model.company_name.Company_NameModel;
import com.hzhr.iot.user.sdk.model.dept.DeptModel;
import com.hzhr.iot.user.sdk.service.User_Company_NameService;
import com.hzhr.iot.user.sdk.service.User_DeptService;
import com.hzhr.iot.web.Global;
import com.hzhr.iot.web.controller.base.BaseController;
import com.hzhr.iot.web.model.response.BaseResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value="部门接口",description="部门接口")
@RestController
@RequestMapping("/dept")
public class DeptController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    User_DeptService user_deptService;
    @Autowired
    User_Company_NameService user_company_nameService;
    @Autowired
    Global global;

    @ApiOperation(value = "部门列表", notes = "部门列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页码",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "单页面条数", required =false,dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "companyId", value = "公司ID",required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "部门编号",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "begintime", value = "开始时间，形如2018-01-01 23:00:00", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "endtime", value = "结束时间，形如2018-01-01 23:00:00", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "name", value = "名称", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "isvalid", value = "状态,1启用 0停用 -1全部", required =false,dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "uat", value = "用户登录令牌", dataType = "String", required = true, paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父级ID", dataType = "String", required = true, paramType = "query")}
    )
    @RequestMapping(value = "/getDeptList", method = RequestMethod.POST)
    BaseResponse GetListByCompanyId(@RequestParam(value ="pageNum", required = false,defaultValue = "1") int pageNum,
                                    @RequestParam(value = "pageSize", required = false,defaultValue = "10") int pageSize,
                                    @RequestParam(value = "companyId", required =true,defaultValue="0") String companyId,
                                    @RequestParam(value = "isvalid", required = false,defaultValue="-1") int isvalid,
                                    @RequestParam(value = "code", required = false,defaultValue="") String code,
                                    @RequestParam(value = "name", required = false,defaultValue="") String name,
                                    @RequestParam(value = "begintime", required = false,defaultValue = "") String begintime,
                                    @RequestParam(value = "endtime", required = false,defaultValue = "") String endtime,
                                    @RequestParam(value = "uat", required = true) String uat,
                                    @RequestParam(value = "parentId", required = false,defaultValue="-1") int parentId) throws Exception {
        Map<String, String> header1 = new HashMap<>();
        header1.put("token", global.getToken());
        Map<String, Object> query1 = new HashMap<>();
        query1.put("show", "one");
        query1.put("show_info", true);
        query1.put("equal_id", companyId);
        Company_NameModel company_nameModel = user_company_nameService.get(header1, query1).getModel();

        String token = getToken();
        Map<String, String> header = new HashMap<>();
        header.put("token", token);
        Map<String, Object> query = new HashMap<>();
        query.put("show", "list");
        query.put("show_info", true);
        query.put("page", pageNum);
        query.put("count", pageSize);
        query.put("equal_companyId", companyId);
        query.put("lessthanorequal_level", 3);
        List<DeptModel> deptModels = user_deptService.get(header, query).getModelList();

        Map<String, Object> result = new HashMap<>();
        List<Map<String, Object>> dataList = new ArrayList<>();
        for (DeptModel deptModel :
        deptModels) {
            Map<String, Object> data = new HashMap<>();
            data.put("id", HzhrTypeUtil.object2string(deptModel.getId()));
            data.put("name", HzhrTypeUtil.object2string(deptModel.getName()));
            data.put("code", HzhrTypeUtil.object2string(deptModel.getCode()));
            data.put("level", HzhrTypeUtil.object2string(deptModel.getLevel()));
            if (deptModel.getParentId() != null && deptModel.getParentId().equals(0)) {
                data.put("parentId", null);
            } else {
                data.put("parentId", HzhrTypeUtil.object2string(deptModel.getParentId()));
            }
            data.put("parentname", HzhrTypeUtil.object2string(deptModel.getParentName()));
            data.put("companyId", HzhrTypeUtil.object2string(deptModel.getCompanyId()));
            Map<String, Object> userCompany_companyId = new HashMap<>();
            userCompany_companyId.put("id", deptModel.getCompanyId());
            if (company_nameModel != null) {
                userCompany_companyId.put("name", company_nameModel.getName());
            }
            data.put("userCompany_companyId", userCompany_companyId);
            dataList.add(data);
        }
        result.put("dataList", dataList);
        Map<String, Object> company = new HashMap<>();
        company.put("id", companyId);
        if (company_nameModel != null) {
            company.put("name", company_nameModel.getName());
        }
        result.put("company", company);

        result.put("pageNum",pageNum);
        result.put("pageSize",pageSize);
        result.put("total",dataList.size());


//        Map<String, Object> data = new HashMap<>();
//        data.put("id", "12");
//        data.put("name", "集团");
//        data.put("code", "9-01");
//        data.put("level", "1");
//        data.put("parentId", null);
//        data.put("companyId", "9");
//        data.put("companyname", "");
//        data.put("parentname", "");
//        data.put("auditstatus", "1");
//        data.put("isvalid", "1");
//        data.put("createtime", "1532272910313");
//        data.put("updatetime", "1532272910313");
//        data.put("auditreason", "");
//
//        Map<String, String> userCompany_companyId = new HashMap<>();
//        userCompany_companyId.put("id", "dev_!hzhr@_00000000000009");
//        userCompany_companyId.put("name", "重庆燃气集团股份有限公司");
//        userCompany_companyId.put("type", "1");
//        userCompany_companyId.put("code", "GAS1532272715580833");
//        userCompany_companyId.put("auditstatus", "1");
//        userCompany_companyId.put("isvalid", "1");
//        userCompany_companyId.put("contacts", "");
//        userCompany_companyId.put("phone", "");
//        userCompany_companyId.put("email", "");
//        userCompany_companyId.put("isauth", "0");
//        userCompany_companyId.put("userId", "dev_!hzhr@_00000000000014");
//        userCompany_companyId.put("createtime", "1532272715581");
//        userCompany_companyId.put("updatetime", "1532272715581");
//        userCompany_companyId.put("auditreason", "");
//        userCompany_companyId.put("audittime", "1532272715581");
//        userCompany_companyId.put("addr", "重庆市江北区鸿恩路");
//        data.put("userCompany_companyId", userCompany_companyId);
//        dataList.add(data);
//        result.put("dataList", dataList);
//        result.put("pageNum",1);
//        result.put("pageSize",1);
//        result.put("total",1);

        return new BaseResponse("0", "成功", result);
    }
}
